package com.qf.dao.impl;

import com.qf.dao.StageDao;
import com.qf.pojo.Project;
import com.qf.pojo.Stage;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;


public class StageImpl implements StageDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

        this.jdbcTemplate = jdbcTemplate;
    }
    @Override
    public void addStage(Stage stage) {
        int i = jdbcTemplate.update("insert into stage values (?,?,?,?,?,?,?)", stage.getSid(), stage.getSname(), stage.getStarttime(), stage.getEndtime(), stage.getPid(),stage.getUpdatetime(), stage.getStatus());
    }

    @Override
    public List<Stage> findAll() {
        List<Stage> stages = jdbcTemplate.query("select * from stage", new BeanPropertyRowMapper<>(Stage.class));
        for (Stage stage:stages) {
            List<Project> projects = jdbcTemplate.query("select * from project where pid = ?", new BeanPropertyRowMapper<>(Project.class),stage.getPid());
            stage.setProjects(projects);
        }
        return stages;
    }
}
